﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FuncWorks.XNA.XTiled;
using Microsoft.Xna.Framework;
using fess.RPG;
using fess.Graphics;
using Microsoft.Xna.Framework.Graphics;

namespace fess.Maps
{
  class Tavern : GenericMap
  {
    List<ActionCharacter> tavernGuards = new List<ActionCharacter>();
    ActionCharacter owner;
    List<Vector2> attackPoints;

    public Tavern(FessGame game)
      : base(game, game.Content.Load<Map>("tavern"))
    {
      drawObjects = true;

      var guyCoords = (from mo in map.ObjectLayers["stuff"].MapObjects
                       where mo.Type == "place"
                       orderby int.Parse(mo.Name)
                       select mo.Bounds.Center.ToVector2()).ToList();

      var dirs = (from mo in map.ObjectLayers["stuff"].MapObjects
                  where mo.Type == "direction"
                  orderby mo.Name
                  select mo.Bounds.Center.ToVector2()).ToList();

      attackPoints = (from mo in map.ObjectLayers["stuff"].MapObjects
                      where mo.Type == "attack"
                      select mo.Bounds.Center.ToVector2()).ToList();

      var names = new[] { "Фингон", "Неллас", "Маэглин", "Халдир" };
      var dirs1 = new[] { 5, 2, 3, 4, 6, 1, 1, 2, 4, 4, 4, 4 };

      for (int i = 0; i < 4; ++i)
      {
        var wc = new WalkingCharacter(new CharacterMovementSheet(game.Content.Load<Texture2D>("chars/generic/guy" + (i + 1))));
        var face = game.Content.Load<Texture2D>("chars/generic/guy" + (i + 1) + "-face");
        var name = names[i];

        ActionCharacter guy;
        if (i == 0)
        {
          guy = new TavernOwner(wc, face, name);
          owner = guy;
        }
        else
          guy = new TavernGuy(wc, face, name);

        tavernGuards.Add(guy);

        AddCharacter(guy, guyCoords[i], false, false);
        guy.Character.Direction = dirs[dirs1[i] - 1] - guy.Character.Position;
        guy.AddAI(new GotoAI(guy, guy.Character.Position, dirs[dirs1[i] - 1]));
        guy.Damage += RedAlert;
      }

      var atlika = new Atlika(game);
      AddCharacter(atlika, guyCoords[4], false, false);
      atlika.Character.Direction = dirs[5] - atlika.Character.Position;
      var fakeFace = game.Content.Load<Texture2D>("chars/generic/guy1-face");
      atlika.AddAI(new GotoAI(atlika, atlika.Character.Position, dirs[5]));
      tavernGuards.Add(atlika);
      atlika.Damage += RedAlert;

      for (int i = 5; i < 10; ++i)
      {
        var wc = new WalkingCharacter(new CharacterMovementSheet(game.Content.Load<Texture2D>("chars/generic/guy" + (i + 1))));
        var face = fakeFace;
        var name = names[0];

        ActionCharacter guy = new ActionCharacter(wc, face, name, 2, 2, 10, 0.9);
        AddCharacter(guy, guyCoords[i], false, false);
        guy.Character.Direction = dirs[dirs1[i] - 1] - guy.Character.Position;
        guy.Damage += RedAlert;
      }

      var girl = new ActionCharacter(new WalkingCharacter(new CharacterMovementSheet(game.Content.Load<Texture2D>("chars/generic/girl1"))),
        fakeFace, names[0], 2, 2, 10, 0.9);
      AddCharacter(girl, guyCoords[10], false, false);
      girl.Character.Direction = dirs[dirs1[10] - 1] - girl.Character.Position;
      girl.Damage += RedAlert;
    }

    void RedAlert()
    {
      owner.Say("Неясыть буянит, пора его УПОКОИТЬ!");
      foreach (var c in tavernGuards)
      {
        ChangeFaction(c, true);
        if (c.Mana == 0)
          c.AddAI(new MeleeAI(c, true));
        else
          c.AddAI(new MageAI(c, true, attackPoints));
      }
    }

    public override void Activate()
    {
      game.fess.Character.Position = new Vector2(3, 9) * 32;
      base.Activate();
    }

    protected override bool ExitEngaged(MapObject exit)
    {
      List<Answer> destinations = new List<Answer>();
      destinations.Add(new Answer("Домой", () => game.home.Activate()));
      if (game.CemeteryQuest != FessGame.CemeteryQuestStatus.Unknown)
        destinations.Add(new Answer("Кладбище", () => game.cemetery.Activate()));
      if (game.CaveQuest != FessGame.CaveQuestStatus.Unknown)
        destinations.Add(new Answer("Пещера", () => game.caveOut.Activate()));

      game.AddActivity(new Dialog(game.fess,
          () => { },
          () => { },
        new Dialog.Line(game.fess, "Куда теперь?", destinations.ToArray())));

      return true;
    }
  }
}
